package com.gameplatform.service;

import com.gameplatform.model.Comment;
import com.gameplatform.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CommentService {
    
    public List<Comment> getAllComments() {
        List<Comment> comments = new ArrayList<>();
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT c.*, u.username FROM comments c JOIN users u ON c.user_id = u.id ORDER BY c.create_time DESC";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            
            while (rs.next()) {
                Comment comment = new Comment();
                comment.setId(rs.getInt("id"));
                comment.setGameId(rs.getInt("game_id"));
                comment.setUserId(rs.getInt("user_id"));
                comment.setContent(rs.getString("content"));
                comment.setCreateTime(rs.getTimestamp("create_time"));
                comment.setUsername(rs.getString("username"));
                comments.add(comment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(conn);
        }
        
        return comments;
    }
    
    public List<Comment> getCommentsByGameId(int gameId) {
        List<Comment> comments = new ArrayList<>();
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT c.*, u.username FROM comments c JOIN users u ON c.user_id = u.id WHERE c.game_id = ? ORDER BY c.create_time DESC";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, gameId);
            ResultSet rs = stmt.executeQuery();
            
            while (rs.next()) {
                Comment comment = new Comment();
                comment.setId(rs.getInt("id"));
                comment.setGameId(rs.getInt("game_id"));
                comment.setUserId(rs.getInt("user_id"));
                comment.setContent(rs.getString("content"));
                comment.setCreateTime(rs.getTimestamp("create_time"));
                comment.setUsername(rs.getString("username"));
                comments.add(comment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(conn);
        }
        
        return comments;
    }
    
    public boolean createComment(Comment comment) {
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO comments (game_id, user_id, content, create_time) VALUES (?, ?, ?, NOW())";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, comment.getGameId());
            stmt.setInt(2, comment.getUserId());
            stmt.setString(3, comment.getContent());
            
            int result = stmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }
    
    public boolean deleteComment(int id) {
        Connection conn = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM comments WHERE id = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            
            int result = stmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }
}